#include <avr/io.h>
#include <avr/iom164.h> 
#include <avr/interrupt.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void power_on ();
void get_data();
void serial_init();
void lcd_transmit(unsigned char data);
char lcd_receive();
void lcd_flush();
int main(void){
	serial_init(); // Initialize Serial
	power_on(); // Turn Power Light On

	while(1){
		get_data();
	}		
} 

void power_on (){
	DDRA=0xff;
	DDRD=0xff;
	PORTA = 0x01;
}

void get_data (){
	int flag = 0;
	unsigned char data;
	data = lcd_receive();
	lcd_transmit(data);
	if(flag == 1){
		PORTA = 0x01;
		flag = 0;
	}
	else if(flag == 0){
		PORTA = 0x02;
		flag = 1;
	}
	 
}
void serial_init(){
	PRR0 = (0<<PRUSART0); // Power Reduction Off
	UBRR0H = (unsigned char)(9600>>8);
	UBRR0L = (unsigned char)(9600);
	UCSR0B = (1<<RXEN0)|(1<<TXEN0); // Allow transmit and receive
	UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}

void lcd_transmit (unsigned char data){
	while( !(UCSR0A & (1<<UDRE0)));
	UDR0 = data;
}

char lcd_receive (void){
	while (!(UCSR0A & (1<<RXC0)));
	return UDR0;
}	

void lcd_flush (void){
	char tmp;
	while (UCSR0A & (1<<RXC0) ) tmp = UDR0;
}
